{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "import pandas as pd\r\n",
    "import spider as msd\r\n"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "city = pd.read_json('CityCode.json')\r\n",
    "city\r\n"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "        省代码  省名称   地级市代码 地级市名称\n",
       "0    110000  北京市  110100   北京市\n",
       "1    120000  天津市  120100   天津市\n",
       "2    130000  河北省  130600   保定市\n",
       "3    130000  河北省  130900   沧州市\n",
       "4    130000  河北省  130800   承德市\n",
       "..      ...  ...     ...   ...\n",
       "384  710000  台湾省  719006   苗栗县\n",
       "385  710000  台湾省  719004   新竹县\n",
       "386  710000  台湾省  719014   澎湖县\n",
       "387  810000   香港  810000    香港\n",
       "388  820000   澳门  820000    澳门\n",
       "\n",
       "[389 rows x 4 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>省代码</th>\n",
       "      <th>省名称</th>\n",
       "      <th>地级市代码</th>\n",
       "      <th>地级市名称</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>110000</td>\n",
       "      <td>北京市</td>\n",
       "      <td>110100</td>\n",
       "      <td>北京市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>120000</td>\n",
       "      <td>天津市</td>\n",
       "      <td>120100</td>\n",
       "      <td>天津市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>130000</td>\n",
       "      <td>河北省</td>\n",
       "      <td>130600</td>\n",
       "      <td>保定市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>130000</td>\n",
       "      <td>河北省</td>\n",
       "      <td>130900</td>\n",
       "      <td>沧州市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>130000</td>\n",
       "      <td>河北省</td>\n",
       "      <td>130800</td>\n",
       "      <td>承德市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>384</th>\n",
       "      <td>710000</td>\n",
       "      <td>台湾省</td>\n",
       "      <td>719006</td>\n",
       "      <td>苗栗县</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>385</th>\n",
       "      <td>710000</td>\n",
       "      <td>台湾省</td>\n",
       "      <td>719004</td>\n",
       "      <td>新竹县</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>386</th>\n",
       "      <td>710000</td>\n",
       "      <td>台湾省</td>\n",
       "      <td>719014</td>\n",
       "      <td>澎湖县</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>387</th>\n",
       "      <td>810000</td>\n",
       "      <td>香港</td>\n",
       "      <td>810000</td>\n",
       "      <td>香港</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>388</th>\n",
       "      <td>820000</td>\n",
       "      <td>澳门</td>\n",
       "      <td>820000</td>\n",
       "      <td>澳门</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>389 rows × 4 columns</p>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "listProvinceName = list(city[\"省名称\"].unique())\r\n",
    "listProvinceName\r\n"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['北京市',\n",
       " '天津市',\n",
       " '河北省',\n",
       " '山西省',\n",
       " '内蒙古自治区',\n",
       " '辽宁省',\n",
       " '吉林省',\n",
       " '黑龙江省',\n",
       " '上海市',\n",
       " '江苏省',\n",
       " '浙江省',\n",
       " '安徽省',\n",
       " '福建省',\n",
       " '江西省',\n",
       " '山东省',\n",
       " '河南省',\n",
       " '湖北省',\n",
       " '湖南省',\n",
       " '广东省',\n",
       " '广西壮族自治区',\n",
       " '海南省',\n",
       " '重庆市',\n",
       " '四川省',\n",
       " '贵州省',\n",
       " '云南省',\n",
       " '西藏自治区',\n",
       " '陕西省',\n",
       " '甘肃省',\n",
       " '青海省',\n",
       " '宁夏回族自治区',\n",
       " '新疆维吾尔自治区',\n",
       " '台湾省',\n",
       " '香港',\n",
       " '澳门']"
      ]
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "start_date = \"20201204\"\r\n",
    "end_date = \"20201210\"\r\n",
    "dateRange = pd.date_range(start_date, end_date).strftime(\"%Y-%m-%d\").tolist()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "from tqdm import tqdm\r\n",
    "batchdata = []\r\n",
    "with tqdm(total=len(listProvinceName)) as pbar:\r\n",
    "    for i in listProvinceName:\r\n",
    "        a = msd.GetBatchDateData('province', 'province', i, migrationType='in',\r\n",
    "                            date=dateRange)\r\n",
    "        batchdata.append(a)\r\n",
    "        pbar.update(1)\r\n",
    "batchdata\r\n"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "source": [
    "writer = pd.ExcelWriter('chinaProvince.xlsx')\r\n",
    "for j in batchdata:\r\n",
    "    j.to_excel(writer,j.iloc[0][2])\r\n",
    "writer.save()\r\n"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "batchdata[0]"
   ],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.8.8",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.8 64-bit ('base': conda)"
  },
  "interpreter": {
   "hash": "b3ba2566441a7c06988d0923437866b63cedc61552a5af99d1f4fb67d367b25f"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}